package cn.tianyun.pethome.login.service.impl;

import cn.tianyun.pethome.basic.exception.BusinessException;
import cn.tianyun.pethome.basic.util.BaseConstants;
import cn.tianyun.pethome.basic.util.loginUtil.StrUtils;
import cn.tianyun.pethome.basic.util.loginUtil.VerifyCodeUtils;
import cn.tianyun.pethome.login.domain.PhoneCodeVo;
import cn.tianyun.pethome.login.service.ImgCodeCreatService;
import cn.tianyun.pethome.org.domain.User;
import cn.tianyun.pethome.org.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

@Service
public class ImgCodeCreatServiceImpl implements ImgCodeCreatService {
   @Autowired
   private RedisTemplate redisTemplate;
   @Autowired
   private UserMapper userMapper;

   /*
   图片验证码，
   * */
    @Override
    public String getImgCode(String uuid ,String key) {
       try {
           String code = VerifyCodeUtils.generateVerifyCode(4).toLowerCase();
           redisTemplate.opsForValue().set(uuid+key,code,5, TimeUnit.MINUTES);
           return  VerifyCodeUtils.VerifyCode(110, 42, code);
       }catch (Exception e){
           e.printStackTrace();
       }
        return null;
    }
    @Override
    public void register(PhoneCodeVo phoneCodeVo) {
       /*参数验证*/
        if(phoneCodeVo.getMobile()==null){
            throw  new BusinessException("手机号不能为空");
        }
        if(phoneCodeVo.getImageCode()==null){
            throw  new BusinessException("图片验证码不能为空");
        }
        if(phoneCodeVo.getImageCodeKey()==null){
            throw  new BusinessException("参数错误");
        }
        /*验证验证码是否过期或者输入错误*/
        String imgCode = (String)redisTemplate.opsForValue().get(phoneCodeVo.getImageCodeKey() + BaseConstants.ImgCode.STATE_REGISTER);
        System.out.println("===>"+imgCode);
       if(imgCode==null){
           throw  new BusinessException("验证码已过期");
       }
        if(!imgCode.equals(phoneCodeVo.getImageCode())){
           throw  new BusinessException("验证码错误，请检查");
       }
        /*验证手机号是否已经被注册*/
        User user= userMapper.loadByPhone(phoneCodeVo.getMobile());
        if(user!=null){
            throw  new BusinessException("该号码已经被注册，请登录");
        }
        /*手机号没有被注册*/
        String KeyCheck = phoneCodeVo.getMobile()+BaseConstants.ImgCode.STATE_REGISTER_CHECK ;
        String key= phoneCodeVo.getMobile()+BaseConstants.ImgCode.STATE_REGISTER;
        Object o = redisTemplate.opsForValue().get(KeyCheck);/*获取1分钟那个验证码*/
        if(o==null){
            String value="";
            Object PhoneCode = redisTemplate.opsForValue().get(key);/*获取三分钟那个验证码*/
            if(PhoneCode!=null){
                value=PhoneCode.toString();
                System.out.println(value);
            }else {
                value = StrUtils.getRandomString(6);/*生成6位数字的验证码*/
                redisTemplate.opsForValue().set(key,value,3,TimeUnit.MINUTES);/*保存数字验证码。过期时间为三分钟*/
                redisTemplate.opsForValue().set(KeyCheck,"",1,TimeUnit.MINUTES);/*保存一个手机号和业务键为key，值为“”，                                                                                  过期时间为一分钟的的数据*/
                System.out.println("发送短信验证码："+value);
            }
        }else {
            throw  new BusinessException("验证码已发送，请输入");
        }
    }
}
